'use strict'
import React from "react";
import {render} from 'react-dom';
import {Link} from 'react-router';
import Simditor from 'simditor';
//引入工具类
import MMALL_COM from 'util/comm.jsx';

//引入scss
import './index.scss';
var _comm = new MMALL_COM();
class RichEditor extends React.Component{
    constructor(props){
        super(props);
        this.state={

        }
    }
    componentDidMount(){
        this.initEditor();
    }
    //初始化富文本组件
    initEditor(){
        let config = {
            placeholder: this.props.placeholder,
            params: {},
            tabIndent: true,
            toolbar: [
                'title',
                'bold',
                'italic',
                'underline',
                'strikethrough',
                'fontScale',
                'color',
                'link',
                'hr',
                'image',
                'indent',
                'outdent',
                'alignment',
            ],
            upload: {
                url: _comm.getServerHost('/manage/product/richtext_img_upload.do'), //文件上传的接口地址
                defaultImage: '',
                fileKey: 'upload_file', //服务器端获取文件数据的参数名
                connectionCount: 3,
                leaveConfirm: '正在上传文件',
            },

            toolbarFloat: true,
            toolbarFloatOffset: 0,
            toolbarHidden: false,
            pasteImage: false,
            cleanPaste: false,
            textarea: this.refs['textarea']
        };
        this.editor = new Simditor(config);// 初始化编辑器
        this.editor.setValue(this.props.value);

        //监听改变
        this.editor.on("valuechanged", e=> {
            this.props.onValueChange(this.editor.getValue());;
        });

        //更改图片上传类型
        $(".simditor input[type='file']").attr('accept', 'image/jpg,image/jpeg,image/png,image/bmp');
    }
    //设置value值
    setValue(value){
        this.editor.setValue(value);
    }
    render(){
        return (
            <div className="rich-editor">
                <textarea ref="textarea"></textarea>
            </div>
        );
    }
}
export default RichEditor;
